package tests;

import java.sql.Date;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;
import server.DBLogic;

import common.Profile;

public class DBLogicTest extends TestCase {

	
	Profile profile1;
	Profile profile2;
	DBLogic DBL;
	List<String> rivals;
	
	@SuppressWarnings("deprecation")
	protected void setUp() throws Exception {
		profile1 = new Profile("Amit","abcd","bla@bla.co.il");
		profile2 = new Profile("Alon","abcdef","bl2a@bla.co.il");
		rivals = new ArrayList<String>();
		rivals.add("Alon");
		rivals.add("Nir");
		profile1.addHistoryEntry(new Date(11, 2, 2010),rivals,2);
		rivals.add("Oded");
		profile1.addHistoryEntry(new Date(5, 8, 2010), rivals, 3);
		DBL = new DBLogic();
		DBL.InitDBConnection();
	}

	protected void tearDown() throws Exception {
		DBL.deleteProfile(profile1.getUserName());
		DBL.deleteProfile(profile2.getUserName());
		DBL.ShutDBConnection();
	}
	
	public void testAddAndQueryNewProfile() throws SQLException {
		assertTrue(DBL.addNewProfile(profile1));
		assertEquals(profile1.getEmail(),DBL.queryProfile(profile1.getUserName()).getEmail());
		assertEquals(profile1.getPassword(),DBL.queryProfile(profile1.getUserName()).getPassword());
		assertEquals(profile1.getUserName(),DBL.queryProfile(profile1.getUserName()).getUserName());
		assertEquals(profile1.getRating(),DBL.queryProfile(profile1.getUserName()).getRating());
		assertEquals(null,DBL.queryProfile(profile2.getUserName()));
		
		Profile temp = DBL.queryProfile(profile1.getUserName());
		assertEquals(temp.getHistory().getNumOfHistories(),2);
		int count1=0;
		for (int i=0;i<temp.getHistory().getNumOfHistories();i++) {
			count1 += temp.getHistory().getEntry(i).getScore();
		}
		int count2=0;
		for (int i=0;i<profile1.getHistory().getNumOfHistories();i++) {
			count2 += profile1.getHistory().getEntry(i).getScore();
		}
		assertEquals(count1,count2);
		
		int i = 0;
		for (String rival : temp.getHistory().getEntry(1).getRivals()) {
			assertEquals(rival,rivals.get(i));			
			i++;
		}
		i = 0;
		for (String rival : temp.getHistory().getEntry(0).getRivals()) {
			assertEquals(rival,rivals.get(i));			
			i++;
		}
		assertTrue(DBL.addNewProfile(profile2));
		assertEquals(profile2.getEmail(),DBL.queryProfile(profile2.getUserName()).getEmail());
		assertEquals(profile2.getPassword(),DBL.queryProfile(profile2.getUserName()).getPassword());
		assertEquals(profile2.getUserName(),DBL.queryProfile(profile2.getUserName()).getUserName());
		assertEquals(profile2.getRating(),DBL.queryProfile(profile2.getUserName()).getRating());
		assertEquals(DBL.queryProfile(profile2.getUserName()).getHistory().getNumOfHistories(),0);
	}
	
	@SuppressWarnings("deprecation")
	public void testUpdateProfile() throws SQLException {
		assertTrue(DBL.addNewProfile(profile1));
		profile1.setEmail("alon@ballon.com");
		Profile temp = DBL.queryProfile(profile1.getUserName());
		assertNotEquals(temp.getEmail(),"alon@ballon.com");
		assertNotEquals(temp.getEmail(),profile1.getEmail());
		assertTrue(DBL.updateProfile(profile1));
		temp = DBL.queryProfile(profile1.getUserName());
		assertEquals(temp.getEmail(),"alon@ballon.com");
		assertEquals(temp.getEmail(),profile1.getEmail());
		
		profile1.setPassword("ga");
		assertTrue(DBL.updateProfile(profile1));
		temp = DBL.queryProfile(profile1.getUserName());
		assertEquals(temp.getPassword(),profile1.getPassword());
		
		int oldRating = profile1.getRating();
		profile1.increaseRating(100);
		assertTrue(DBL.updateProfile(profile1));
		temp = DBL.queryProfile(profile1.getUserName());
		assertEquals(temp.getRating(),oldRating+100);
		
		assertEquals(profile1.getHistory().getNumOfHistories(),2);
		profile1.addHistoryEntry(new Date(1, 1, 2009), rivals, 9);
		assertTrue(DBL.updateProfile(profile1));
		temp = DBL.queryProfile(profile1.getUserName());
		assertEquals(profile1.getHistory().getNumOfHistories(),3);
		int i = 0;
		for (String rival : temp.getHistory().getEntry(2).getRivals()) {
			assertEquals(rival,rivals.get(i));			
			i++;
		}
		int count1=0;
		for (i=0;i<temp.getHistory().getNumOfHistories();i++) {
			count1 += temp.getHistory().getEntry(i).getScore();
		}
		int count2=0;
		for (i=0;i<profile1.getHistory().getNumOfHistories();i++) {
			count2 += profile1.getHistory().getEntry(i).getScore();
		}
		assertEquals(count1,count2);
	}

	private void assertNotEquals(String str1, String str2) {
		assertTrue(str1.compareTo(str2)!=0);
	}

	public void testDeleteProfile() throws SQLException {
		assertTrue(DBL.addNewProfile(profile1));
		assertTrue(DBL.deleteProfile(profile1.getUserName())==1);
		assertTrue(DBL.deleteProfile(profile1.getUserName())==0);
		assertTrue(DBL.deleteProfile(profile2.getUserName())==0);
		
		assertTrue(DBL.addNewProfile(profile2));
		assertTrue(DBL.addNewProfile(profile1));
		assertTrue(null != DBL.queryProfile(profile1.getUserName()));
		assertTrue(null != DBL.queryProfile(profile2.getUserName()));
		assertTrue(DBL.deleteProfile(profile1.getUserName())==1);
		assertEquals(null,DBL.queryProfile(profile1.getUserName()));
		assertTrue(null != DBL.queryProfile(profile2.getUserName()));
		assertTrue(DBL.deleteProfile(profile2.getUserName())==1);
		assertEquals(null,DBL.queryProfile(profile1.getUserName()));
		assertEquals(null,DBL.queryProfile(profile2.getUserName()));
	}

}
